<?php
namespace Admin\Controller;
use Admin\Api\WxmenuApi;
use Think\Controller;
use Admin\Api\CarTermApi;
use Weixin\Api\WxaccountApi;

class WxmenuController extends BaseController {

    /*
     * 菜单列表
     * */
    public function index(){
        $map = array('wxaccount_id' => '6');
        $page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
        $order = " sort desc ";
        //
        $result = apiCall(WxmenuApi::QUERY, array($map, $page, $order));
        if ($result['status']) {
            $this -> assign("show", $result['info']['show']);
            $this -> assign("list", $result['info']['list']);
            $this -> display();
        } else {
            $this -> error($result['info']);
        }
       $this->display();
    }

    /*
     *增加菜单
     * */
    public function add(){

        if(IS_POST){
            $entity = array('name' => I('post.name', ''), //菜单名称
                'menukey' => I('post.key', ''), 'url' => I('post.url', ''), 'sort' => I('post.sort', '1'), 'wxaccount_id' => '6', 'pid' => I('post.pid', 0), );
            if (!empty($entity['url'])) {
                $entity['type'] = "view";
            } else {
                $entity['type'] = 'click';
            }

            $result = apiCall(WxmenuApi::ADD, array($entity));
            if ($result['status']) {
                $this -> success(L('RESULT_SUCCESS'), U('Admin/Wxmenu/index'));
            } else {
                $this -> error($result['info']);
            }
        }else{

            $map = array('wxaccount_id' => 1, 'pid' => 0);

            $result = apiCall(WxmenuApi::QUERY_NO_PAGING, array($map, "sort desc", "name,id"));
            if ($result['status']) {
                $this -> assign("mainmenus", $result['info']);
                $this -> display();
            } else {
                $this -> error($result['info']);
            }
        }

    }

    /*
     * 删除菜单
     * */
    public function delete(){
        $this->display();
    }

    /*
     * 编辑菜单
     * */
    public function edit(){
        if(IS_POST){

        }else{
            $this->display();
        }
    }

    /**
     * 发送到微信服务器
     */
    public function sendToWXServer() {
        if (IS_GET) {
            $map = array('wxaccount_id' => '6');
            $result = apiCall(WxmenuApi::QUERY_NO_PAGING, array($map, "sort desc", "name,id,pid,menukey,url,type"));
            if ($result['status']) {
                $menulist = $result['info'];
                if (count($menulist) > 15) {
                    $this -> error("菜单不能超过15个了！");
                }

                $convertML = $this -> convertMenu($menulist);

                $result = apiCall(WxaccountApi::GET_INFO, array( array('id' => '6')));

                if ($result['status']) {
                    $appid = $result['info']['appid'];
                    $appsecret = $result['info']['appsecret'];

                    $weixinApi = new \Common\Api\WeixinApi($appid, $appsecret);

                    $result = $weixinApi -> createMenu($convertML);
                    if ($result['status']) {
                        $this -> success(L("RESULT_SUCCESS"));
                    } else {

                        $this -> error($result['msg']);
                    }
                    //

                } else {

                    $this -> error($result['info']);
                }

            } else {

                $this -> error($result['info']);
            }
        }
    }


    /**
     * 转换为可处理的数据
     */
    private function convertMenu($menulist) {
        $convertMl = array();

        //主菜单
        foreach ($menulist as $key => $menu) {
            if ($menu['pid'] == 0) {
                $convertMl[$menu['id']] = array('name' => $menu['name'], 'type' => $menu['type'], 'url' => $menu['url'], 'key' => $menu['menukey'], '_child' => array());
            }
        }

        //子菜单
        foreach ($menulist as $key => $menu) {
            if ($menu['pid'] > 0 && isset($convertMl[$menu['pid']])) {
                //子菜单
                array_push($convertMl[$menu['pid']]['_child'], array('name' => $menu['name'], 'type' => $menu['type'], 'url' => $menu['url'], 'key' => $menu['menukey'], ));
            }
        }

        return $convertMl;
    }
}